package com.collect.cameroon.zhiScene.zhiCollection.zhiAdmin;

import android.annotation.SuppressLint;
import android.app.Activity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.collect.cameroon.R;
import com.collect.cameroon.zhiAdater.zhiAdminGroupDetail.ZHIGroupMoneyDetailAdjuster;
import com.collect.cameroon.zhiBase.BaseFragment;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRGroupPerformanceMoneyEntity;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRGroupPerformanceOrderEntity;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRGroupUserEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISendEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISGroupUserPerformanceEntity;
import com.collect.cameroon.zhiHelper.ZHIMainProcessHelper;
import com.collect.cameroon.zhiScene.zhiCollection.zhiAdmin.zhiSort.ZHIAdminCompareByAmountASC;
import com.collect.cameroon.zhiScene.zhiCollection.zhiAdmin.zhiSort.ZHIAdminCompareByAmountDES;
import com.collect.cameroon.zhiScene.zhiWidget.zhiFilterAdminGroup.ZHIFilterAdminGroupClickListener;
import com.collect.cameroon.zhiScene.zhiWidget.zhiFilterAdminGroup.ZHIFilterAdminGroupMoney;
import com.collect.cameroon.zhiUtils.StringUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class ZHIAdminGroupMoneyFragment extends BaseFragment<ZHIAdminPresenter> implements ZHIAdminContract.View {

    @BindView(R.id.drawer_layout)
    DrawerLayout drawer_layout;

    @BindView(R.id.drawer_filter_admin_grup_money)
    ZHIFilterAdminGroupMoney drawer_filter_admin_grup_money;

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.tv_page_title)
    TextView tv_page_title;

    @BindView(R.id.tv_cumulative_bonus)
    TextView tv_cumulative_bonus;

    @BindView(R.id.rc_detail_list)
    RecyclerView rc_detail_list;

    @BindView(R.id.iv_sort)
    ImageView iv_sort;

    @BindView(R.id.iv_note)
    ImageView iv_note;
    @BindView(R.id.iv_filter)
    ImageView iv_filter;

    private final ZHIGroupMoneyDetailAdjuster mZHIGroupMoneyDetailAdjuster = new ZHIGroupMoneyDetailAdjuster();

    private ArrayList<ZHIRGroupPerformanceMoneyEntity> mGroupPerformanceMoneyEntityArrayList;

    /**
     * 是否为降序
     */
    private boolean mIsArraySortDesNow = true;

    private ZHIAdminFragmentInterface mZHIAdminFragmentInterface;

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_admin_group_money_v3;
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void initView(View view) {
        try {
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));

            iv_note.setOnClickListener(v -> {
                if (mZHIAdminFragmentInterface != null) {
                    mZHIAdminFragmentInterface.onFinish();
                }
            });

            iv_sort.setOnClickListener(v -> {
                if (mGroupPerformanceMoneyEntityArrayList != null) {
                    if(mIsArraySortDesNow){
                        Collections.sort(mGroupPerformanceMoneyEntityArrayList, new ZHIAdminCompareByAmountASC());
                        mZHIGroupMoneyDetailAdjuster.notifyDataSetChanged();
                        mIsArraySortDesNow = false;
                    }else {
                        Collections.sort(mGroupPerformanceMoneyEntityArrayList, new ZHIAdminCompareByAmountDES());
                        mZHIGroupMoneyDetailAdjuster.notifyDataSetChanged();
                        mIsArraySortDesNow = true;
                    }
                }
            });

            drawer_filter_admin_grup_money.setZHIFilterAdminGroupClickListener(new ZHIFilterAdminGroupClickListener() {
                @Override
                public void onConfirmClick() {
                    if (drawer_layout.isDrawerOpen(drawer_filter_admin_grup_money)) {
                        drawer_layout.closeDrawer(drawer_filter_admin_grup_money);
                    }

                    refreshData();
                }

                @Override
                public void onCloseClick() {
                    if (drawer_layout.isDrawerOpen(drawer_filter_admin_grup_money)) {
                        drawer_layout.closeDrawer(drawer_filter_admin_grup_money);
                    }
                }
            });

            iv_filter.setOnClickListener(v -> {
                showFilterGroupMoney();
            });

            initEvent();
            initData(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mZHIAdminFragmentInterface = (ZHIAdminFragmentInterface) activity;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onGetGroupUserPerformanceMoney(ArrayList<ZHIRGroupPerformanceMoneyEntity> zhirGroupPerformanceMoneyEntityArrayList, ZHISGroupUserPerformanceEntity zhisGroupUserPerformanceEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            ///////////////////////
            BigDecimal resultDecimal = new BigDecimal("0");
            for (int i = 0; i < zhirGroupPerformanceMoneyEntityArrayList.size(); i++) {
                ZHIRGroupPerformanceMoneyEntity zhirGroupPerformanceMoneyEntity = zhirGroupPerformanceMoneyEntityArrayList.get(i);
                BigDecimal bigDecimal = zhirGroupPerformanceMoneyEntity.getRepayAmountBigDecimal();
                resultDecimal = resultDecimal.add(bigDecimal);
            }
            tv_cumulative_bonus.setText(StringUtil.numberFormatter00(resultDecimal.stripTrailingZeros().toPlainString()));

            ///////////////////////
            Collections.sort(zhirGroupPerformanceMoneyEntityArrayList, new ZHIAdminCompareByAmountDES());
            mGroupPerformanceMoneyEntityArrayList = zhirGroupPerformanceMoneyEntityArrayList;

            mZHIGroupMoneyDetailAdjuster.setNewData(zhirGroupPerformanceMoneyEntityArrayList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetGroupUserPerformanceOrder(ArrayList<ZHIRGroupPerformanceOrderEntity> zhirGroupPerformanceOrderEntityArrayList, ZHISGroupUserPerformanceEntity zhisGroupUserPerformanceEntity) {
        srl_swipe_layout.setRefreshing(false);

    }

    @Override
    public void onGetGroupUsers(ArrayList<ZHIRGroupUserEntity> zhirGroupUserEntityArrayList) {
        srl_swipe_layout.setRefreshing(false);
    }

    @SuppressLint({"InflateParams", "SetTextI18n"})
    public void initEvent() {
        srl_swipe_layout.setOnRefreshListener(() -> initData(false));

        rc_detail_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
        rc_detail_list.setAdapter(mZHIGroupMoneyDetailAdjuster);
    }

    public void initData(boolean showDialog) {
        if (ZHIMainProcessHelper.getInstance().isLevelB3Plus()) {
            ZHISGroupUserPerformanceEntity zhisGroupUserPerformanceEntity = getCaseSearchParams();
            if (zhisGroupUserPerformanceEntity == null) {
                srl_swipe_layout.setRefreshing(false);
                return;
            }

            assert mPresenter != null;
            mPresenter.getGroupUserPerformanceMoney(getActivity(), zhisGroupUserPerformanceEntity, showDialog);
            return;
        }

        assert mPresenter != null;
        mPresenter.getGroupUsers(getActivity(), new ZHISendEntity());
    }

    public void refreshData() {
        initData(true);
    }

    public void showFilterGroupMoney() {
        if (ZHIMainProcessHelper.getInstance().isLevelB3Plus()) {
            if (drawer_layout.isDrawerOpen(drawer_filter_admin_grup_money)) {
                drawer_layout.closeDrawer(drawer_filter_admin_grup_money);
            } else {
                drawer_layout.openDrawer(drawer_filter_admin_grup_money);
            }
        }
    }

    public ZHISGroupUserPerformanceEntity getCaseSearchParams() {
        ZHISGroupUserPerformanceEntity zhisGroupUserPerformanceEntity = new ZHISGroupUserPerformanceEntity();
        zhisGroupUserPerformanceEntity.setLeven(drawer_filter_admin_grup_money.getLevel());
        zhisGroupUserPerformanceEntity.setStartDate(drawer_filter_admin_grup_money.getStartDate());
        zhisGroupUserPerformanceEntity.setEndDate(drawer_filter_admin_grup_money.getEndDate());
        zhisGroupUserPerformanceEntity.setSysUserId(drawer_filter_admin_grup_money.getUser());
        return zhisGroupUserPerformanceEntity;
    }

}
